import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import {Switch,Image, Tag} from 'ant-design-vue';
import { setRoleStatus } from '@/api/demo/system';
import { upApi,downApi } from '@/api/sys/book';
import {uploadApi, uploadImgApi,uploadDingApi} from '@/api/sys/upload';
import { useMessage } from '@/hooks/web/useMessage';
import {getLevelSelectsApi} from "@/api/sys/common";
import {clazzSelectsApi} from "@/api/sys/clazz";

type CheckedType = boolean | string | number;
export const columns: BasicColumn[] = [
  {
    title: '书本名称',
    dataIndex: 'bookName',
    // width: 200,
  },
  {
    title: '作者',
    dataIndex: 'author',
    width: 180,
  },
  {
    title: '章节',
    dataIndex: 'chapter',
    width: 180,
  },
  {
    title: '图片',
    dataIndex: 'img',
    width: 180,
      customRender: ({ record }) => {
        const img = record.img;
        return h(Image, {
          "width":50,
          "height":50,
          "src":img
        } );
      },
  },
  {
    title: '内容模块',
    dataIndex: 'module',
    width: 180,
  },
  {
    title: '推荐人群',
    dataIndex: 'levelNames',
    width: 180,
  },
  // {
  //   title: '类别',
  //   dataIndex: 'type',
  //   width: 120,
  //   customRender: ({ record }) => {
  //     const isRead = record.type;
  //     const enable = ~~isRead === 1;
  //  //   const color = enable ? 'green' : 'red';
  //     const text = enable ? '必读' : '扩展';
  //     return h(Tag, null, () => text);
  //   },
  // },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.state === 1,
        checkedChildren: '启用',
        unCheckedChildren: '停用',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          let data={bookId:record.bookId};

          if(record.state === 1){
            downApi(data).then(() => {
              record.state = 0;
              createMessage.success(`已成功禁用`);
            })
              .catch(() => {
                createMessage.error('修改状态失败');
              })
              .finally(() => {
                record.pendingStatus = false;
              });
          }else{
            upApi(data).then(() => {
              record.state = 1;
              createMessage.success(`已成功启用`);
            })
              .catch(() => {
                createMessage.error('修改状态失败');
              })
              .finally(() => {
                record.pendingStatus = false;
              });
          }
        },
      });
    },
  },

  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },

];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'bookName',
    label: '书本名称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'state',
    label: '状态',
    component: 'Select',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '停用', value: 0 },
      ],
    },
    colProps: { span: 6 },
  },
  // {
  //   label: '班级',
  //   field: 'clazzId',
  //   component: 'ApiSelect',
  //   componentProps: {
  //     api: clazzSelectsApi,
  //     resultField:"data",
  //     labelField: 'clazzName',
  //     valueField: 'clazzId',
  //   },
  //   colProps: { span: 6 },
  //
  // },
];

export const formSchema: FormSchema[] = [
  {
    field: 'bookName',
    label: '书本名称',
    required: true,
    component: 'Input',
  },
  {
    field: 'author',
    label: '作者',
    component: 'Input',
  },
  // {
  //   field: 'img',
  //   label: '网络图片路径',
  //   component: 'Input',
  // },
  {
    field: 'imgs',
    label: '图片',
    component: 'Upload',
    componentProps: {
      api:uploadImgApi,
      resultField:"data.data",
      maxSize:"20"
    },
  },
  // {
  //   field: 'fileUrl',
  //   label: '书本文件',
  //   component: 'Input',
  // },
  // {
  //   field: 'fileUrls',
  //   label: '书本文件上传',
  //   component: 'Upload',
  //   componentProps: {
  //     api:uploadApi,
  //     resultField:"data.data",
  //     maxSize:"20"
  //   },
  // },
  {
    field: 'fileInfos',
    label: '钉盘文件上传',
    component: 'Upload',
    componentProps: {
      api:uploadDingApi,
      resultField:"data.data",
      maxSize:"20"
    },
  },
  {
    field: 'type',
    label: '类别',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '必读', value: 1 },
        { label: '扩展', value: 2 },
      ],
    },
  },
  {
    field: 'state',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '停用', value: 0 },
      ],
    },
  },
  {
    field: 'module',
    label: '内容模块',
    component: 'Select',
    componentProps: {
      options: [
        { label: '战略知识', value: "战略知识" },
        { label: '管理知识', value: "管理知识" },
        { label: '体系知识', value: "体系知识" },
        { label: '专业知识', value: "专业知识" },
        { label: '企业文化建设', value: "企业文化建设" },
      ],
    },
  },
  {
    label: '推荐人群',
    field: 'levelNamesArr',
    component: 'ApiSelect',
    componentProps: {
      api: getLevelSelectsApi,
      mode:"multiple",
      resultField:"data",
      labelField: 'label',
      valueField: 'value',
    },
    required: false,
  },
  {
    field: 'num',
    label: '阅读人数',
    component: 'Input',
  },
  // {
  //   label: '班级',
  //   field: 'clazzIdArr',
  //   component: 'ApiSelect',
  //   componentProps: {
  //     api: clazzSelectsApi,
  //     mode:"multiple",
  //     resultField:"data",
  //     labelField: 'clazzName',
  //     valueField: 'clazzId',
  //
  //   }
  // },
  {
    label: '章节',
    field: 'chapter',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入章节，多个章节用中文逗号隔开。例:第一章 xx，第二章 xx',
      autoSize: { minRows: 3, maxRows: 6 },
      showCount: true,
    },
  },
  {
    label: '简介',
    field: 'note',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入简介',
      autoSize: { minRows: 3, maxRows: 6 },
      maxlength: 1000,
      showCount: true,
    },
  },

];
